package com.lkd.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lkd.business.MsgHandler;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.MqttCallback;
import com.lkd.emq.MqttService;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompleteHandler implements MsgHandler {


    @Autowired
    private VendingMachineService vendingMachineService;


    /**
     * 完成工单
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        log.info("接受到主题发送的消息:{}",jsonMsg);
        try {
            TaskCompleteContract contract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
            vendingMachineService.updateStatusByTaskType(contract.getInnerCode(), contract.getTaskType());
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
